package com.zhipu.resetbalance.ui.home.system.device;

import android.os.Bundle;
import android.view.View;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.blankj.utilcode.util.SPUtils;
import com.zhipu.resetbalance.R;
import com.zhipu.resetbalance.databinding.ActivityDeviceBinding;
import com.zhipu.resetbalance.ui.BaseActivity;

import org.angmarch.views.NiceSpinner;
import org.angmarch.views.OnSpinnerItemSelectedListener;

import java.util.ArrayList;
import java.util.List;


/**
 * 设备信息
 */
public class DeviceActivity extends BaseActivity<ActivityDeviceBinding> {

    /**
     * VM
     */
    private DeviceViewModel mDeviceViewModel;


    @Override
    public int initContentView(Bundle savedInstanceState) {

        return R.layout.activity_device;
    }


    @Override
    public void initViewModel() {
        super.initViewModel();

        //VM
        mDeviceViewModel = new ViewModelProvider(getViewModelStore(), new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(DeviceViewModel.class);
        binding.setViewModel(mDeviceViewModel);
    }


    @Override
    public void initView() {
        super.initView();


    }


    @Override
    public void initData() {
        super.initData();
        /**
         * 编号
         */
        String[] ftValuesArray = getResources().getStringArray(R.array.specimenNumber);
        List<String> dataset = new ArrayList<>();
        String mSerialNumber = SPUtils.getInstance().getString("SerialNumber");
        for (String number : ftValuesArray){
            if(mSerialNumber.equals(number)){
                dataset.add(0,number);
            }else {
                dataset.add(number);
            }
        }
        binding.NiceSpinner.attachDataSource(dataset);
        /**
         *
         */



    }


    @Override
    public void initViewObservable() {
        super.initViewObservable();
        /**
         * 编号选择
         */
        binding.NiceSpinner.setOnSpinnerItemSelectedListener(new OnSpinnerItemSelectedListener() {
            @Override
            public void onItemSelected(NiceSpinner parent, View view, int position, long id) {
                SPUtils.getInstance().put("SerialNumber", parent.getItemAtPosition(position).toString());
            }
        });
        /**
         * 自动打印
         */
        mDeviceViewModel.mAutomaticPrinting.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                SPUtils.getInstance().put("AutomaticPrinting",aBoolean);
            }
        });

        /**
         * 线性定标
         */
        mDeviceViewModel.mLinearScaling.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                SPUtils.getInstance().put("LinearScaling",aBoolean);
            }
        });

    }
}
